package com.lei.ssh.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
	
    private static final long serialVersionUID = 572146812454l ;
	private static final int BUFFER_SIZE = 16 * 1024 ;

	private File[] uploads;
	private String[] uploadFileNames;
	private String[] uploadContentTypes;

	public File[] getUpload() {
		return this.uploads;
	}

	public void setUpload(File[] upload) {
		this.uploads = upload;
	}

	public String[] getUploadFileName() {
		return this.uploadFileNames;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileNames = uploadFileName;
	}

	public String[] getUploadContentType() {
		return this.uploadContentTypes;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentTypes = uploadContentType;
	}

	public String execute() throws Exception {
		System.out.println("\n\n upload2");
		System.out.println("files:");
		for (int i=0;i <uploads.length;i++) {
//			System.out.println("*** " + uploads[i] + "\t" + uploads[i].length());
			
	        File myFile = new File(ServletActionContext.getServletContext().getRealPath( "/upload" ) + "/" +uploadFileNames[i]);
//	        System.out.println(myFile.getName());
	        /*---复制文件-----*/
//	        copy( uploads[i], myFile);
//	        ExcelReader excelReader =  new ExcelReader(ServletActionContext.getServletContext().getRealPath( "/upload" ) + "/" +uploadFileNames[i]);
	        ExcelReader excelReader =  new ExcelReader(uploads[i]);
	        /*---------start 得到所有数据 ------*/
			/*List<String[]> list = excelReader.getAllData(0);
			for(Iterator<String[]> it=list.iterator();it.hasNext();){
				String[] datas = it.next();
				for(int j = 0;j<datas.length;j++){
					System.out.print(datas[j]+"  ");
				}
				System.out.println();
			}*/
			 /*---------end 得到所有数据 ------*/
	        /*----------start 得到一行数据-----*/
	        String[] rowdata = excelReader.getRowData(1, 0);
	        for(String data:rowdata){
	        	System.out.println(data);
	        }
	        /*----------end 得到一行数据-----*/
		}
		
		/*System.out.println("filenames:");
		for (String n : uploadFileNames) {
			System.out.println("*** " + n);
		}
		System.out.println("content types:");
		for (String c : uploadContentTypes) {
			System.out.println("*** " + c);
		}
		System.out.println("\n\n");*/
		return SUCCESS;
	}
	
	
    private static void copy(File src, File dst)  {
        try  {
           InputStream in = null ;
           OutputStream out = null ;
            try  {                
               in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
               out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
                byte [] buffer = new byte [BUFFER_SIZE];
                while (in.read(buffer) > 0 )  {
                   out.write(buffer);
               } 
            } finally  {
                if ( null != in)  {
                   in.close();
               } 
                 if ( null != out)  {
                   out.close();
               } 
           } 
        } catch (Exception e)  {
           e.printStackTrace();
       } 
   } 
}